import os

import yaml



"""
读取任意yaml文件信息,并返回yaml文件内容
:param yaml_path: yaml文件地址
:return yaml文件
"""
def read_test_yaml(yaml_path):
    with open(os.getcwd() + yaml_path, encoding='utf-8', mode='r') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value

'''
读取extract.yaml文件,如果读到了就返回结果，如果读不到就返回
:param key:yaml文件对应的参数名称
:return 返回key对应的内容
'''
def read_extract_yaml(key):
    try:
        with open(os.getcwd()+"./extract.yml",encoding='utf-8',mode='r') as f:
            value = yaml.load(stream=f,Loader=yaml.FullLoader)
            values = value[key]
    except (KeyError):
        values[key] = ""
    return values[key]

'''
:return 返回整个extract.yml文件
'''
def read_extractall_yaml():
    with open(os.getcwd() + "./extract.yml", encoding='utf-8', mode='r') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value

"""
把data写入到extract.yaml文件中
:param data:一个字典
"""
def write_extract_yaml(data):
    with open(os.getcwd()+"./extract.yml",encoding='utf-8',mode='a') as f:
        yaml.dump(data,stream=f,allow_unicode=True)

"""
清空extract的数据
"""
def clear_extract_yaml():
    with open(os.getcwd()+"./extract.yml",encoding='utf-8',mode='w') as f:
        f.truncate()

"""
读取extract_url文件
:return extract_url文件的内容
"""
def read_url_yaml():
    with open(os.getcwd()+"./extract_url.yml",encoding='utf-8',mode='r') as f:
        value = yaml.load(stream=f,Loader=yaml.FullLoader)
        return value